some webif fixes, support for lists in webif.sh, cleanup
authorFelix Fietkau <[email protected]>
Fri, 30 Sep 2005 22:06:05 +0000 (22:06 +0000)
committerFelix Fietkau <[email protected]>
Fri, 30 Sep 2005 22:06:05 +0000 (22:06 +0000)
SVN-Revision: 2012

openwrt/package/webif/files/usr/lib/webif/form.awk
openwrt/package/webif/files/usr/lib/webif/validate.awk
openwrt/package/webif/files/usr/lib/webif/webif.sh
openwrt/package/webif/files/www/cgi-bin/webif/wireless-config.sh

index 9bca13551cdb4b7ab1dadcf2d9caef84182c0572..8a405bcb98a2ae915edde2c85a9fc3a9be5f2155 100644 (file)
@@ -59,13 +59,20 @@ $1 ~ /^select/ {
        else option_title = $2
        print "<option" option_selected " value=\"" $2 "\">" option_title "</option>"
 }
+($1 ~ /^listedit/) {
+       n = split($4 " ", items, " ")
+       for (i = 1; i <= n; i++) {
+               if (items[i] != "") print "<tr><td width=\"50%\">" items[i] "</td><td>&nbsp;<a href=\"" $3 "?" $2 "remove=" items[i] "\">Remove</a></td></tr>"
+       }
+       print "<tr><td width=\"100%\" colspan="2"><input type=\"text\" name=\"" $2 "add\" value=\"" $5 "\" /><input type=\"submit\" name=\"" $2 "submit\" value=\"Add\" /></td></tr>"
+}
 $1 ~ /^text/ { print "<input id=\"" $2 "\" type=\"text\" name=\"" $2 "\" value=\"" $3 "\" />" $4 }
 $1 ~ /^submit/ { print "<input type=\"submit\" name=\"" $2 "\" value=\"" $3 "\" />" }
 $1 ~ /^helpitem/ { form_help = form_help "<dt>" $2 ":</dt>" }
 $1 ~ /^helptext/ { form_help = form_help "<dd>" $2 "</dd>" }
 $1 ~ /^helplink/ { form_help_link = "<div class=\"more-help\"><a href=\"" $2 "\">more...</a></div>" }
 
-{
+($1 ~ /^checkbox/) || ($1 ~ /^radio/) {
        print $5
 }
 
index c806400ca24360fd2612d1d5455a7080a643f3a9..da765017d23a47ee492fde3d520a03dc9ea7f841 100644 (file)
@@ -64,7 +64,7 @@ $1 == "string" {
 
 $1 == "mac" {
        valid_type = 1
-       if ((value != "") && (value !~ /^[0-9a-fA-F][0-9a-fA-F]*:[0-9a-fA-F][0-9a-fA-F]*:[0-9a-fA-F][0-9a-fA-F]*:[0-9a-fA-F][0-9a-fA-F]*:[0-9a-fA-F][0-9a-fA-F]*:[0-9a-fA-F][0-9a-fA-F]*$/)) {
+       if ((value != "") && (value !~ /^[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]$/)) {
                valid = 0
                verr = "Invalid value"
        }
@@ -111,4 +111,6 @@ valid_type == 1 {
 
 END {
        print output "ERROR=\"" error "\";\n"
+       if (error == "") print "return 0"
+       else print "return 255"
 }
index fd1b63174b76d9e556d8cf47bb88b79f1b5df215..445b234ec6859c71fe18a6b4e64fc1436bea045c 100644 (file)
@@ -4,24 +4,8 @@ cgidir=/www/cgi-bin/webif
 rootdir=/cgi-bin/webif
 indexpage=index.sh
 
-header() {
-       CATEGORY="$1"
-       UPTIME="$(uptime)"
-       LOADAVG="${UPTIME#*load average: }"
-       UPTIME="${UPTIME#*up }"
-       UPTIME="${UPTIME%%,*}"
-       HOSTNAME=$(cat /proc/sys/kernel/hostname)
-       VERSION=$(cat /etc/banner | grep "(")
-       VERSION="${VERSION%% ---*}"
-       SAVED=${SAVED:+: Settings saved}
-       SAVED_TITLE=${ERROR:+: Settings not saved}
-       SAVED_TITLE=${SAVED_TITLE:-$SAVED}
-       ERROR=${ERROR:+<h3>$ERROR</h3><br /><br />}
-       HEAD="${3:+<div class=\"settings-block-title\"><h2>$3$SAVED_TITLE</h2></div>}"
-       FORM="${5:+<form enctype=\"multipart/form-data\" action=\"$5\" method=\"post\">}"
-       SAVEBUTTON="${5:+<p><input type=\"submit\" name=\"action\" value=\"Save changes\" /></p>}"
-       
-       CATEGORIES=$(grep '##WEBIF:category' $cgidir/.categories $cgidir/*.sh 2>/dev/null | awk -F: '
+categories() {
+       grep '##WEBIF:category' $cgidir/.categories $cgidir/*.sh 2>/dev/null | awk -F: '
        BEGIN {
          print "<div id=\"mainmenu\"><h3><strong>Categories:</strong></h3><ul>"
        }
@@ -32,9 +16,11 @@ header() {
        }
        END {
          print "</ul></div>"
-       }' -)
+       }' -
+}
 
-       SUBCATEGORIES=${2:+$(grep -H "##WEBIF:name:$1:" $cgidir/*.sh 2>/dev/null | sed -e 's,^.*/\([a-zA-Z\.\-]*\):\(.*\)$,\2:\1,' | sort -n | awk -F: '
+subcategories() {
+       grep -H "##WEBIF:name:$1:" $cgidir/*.sh 2>/dev/null | sed -e 's,^.*/\([a-zA-Z\.\-]*\):\(.*\)$,\2:\1,' | sort -n | awk -F: '
        BEGIN {
       print "<div id=\"submenu\"><h3><strong>Sub-Categories:</strong></h3><ul>"
        }
@@ -45,7 +31,28 @@ header() {
        END {
       print "</ul></div>"
        }
-  ' -)}
+  ' -
+}
+
+header() {
+       ERROR=${ERROR:+<h3>$ERROR</h3><br /><br />}
+       SAVED=${SAVED:+: Settings saved}
+       _category="$1"
+       _uptime="$(uptime)"
+       _loadavg="${_uptime#*load average: }"
+       _uptime="${_uptime#*up }"
+       _uptime="${_uptime%%,*}"
+       _hostname=$(cat /proc/sys/kernel/hostname)
+       _version=$(cat /etc/banner | grep "(")
+       _version="${_version%% ---*}"
+       _saved_title=${ERROR:+: Settings not saved}
+       _saved_title=${_saved_title:-$SAVED}
+       _head="${3:+<div class=\"settings-block-title\"><h2>$3$_saved_title</h2></div>}"
+       _form="${5:+<form enctype=\"multipart/form-data\" action=\"$5\" method=\"post\">}"
+       _savebutton="${5:+<p><input type=\"submit\" name=\"action\" value=\"Save changes\" /></p>}"
+       _categories=$(categories $1)
+       _subcategories=${2:+$(subcategories $1 $2)}
+
        cat <<EOF
 Content-Type: text/html
 Pragma: no-cache
@@ -63,20 +70,20 @@ Pragma: no-cache
                                <div id="short-status">
                                        <h3><strong>Status:</strong></h3>
                                        <ul>
-                                               <li><strong>Hostname:</strong> $HOSTNAME</li>
-                                               <li><strong>Uptime:</strong> $UPTIME</li>
-                                               <li><strong>Load:</strong> $LOADAVG</li>
-                                               <li><strong>Version:</strong> $VERSION</li>
+                                               <li><strong>Hostname:</strong> $_hostname</li>
+                                               <li><strong>Uptime:</strong> $_uptime</li>
+                                               <li><strong>Load:</strong> $_loadavg</li>
+                                               <li><strong>Version:</strong> $_version</li>
                                        </ul>
                                </div>
                        </div>
-                       $CATEGORIES
-                       $SUBCATEGORIES
+                       $_categories
+                       $_subcategories
                </div>
-               $FORM
+               $_form
                <div id="content">
                        <div class="settings-block">
-                               $HEAD
+                               $_head
                                $ERROR
 EOF
        [ -z "$REMOTE_USER" \
@@ -127,9 +134,9 @@ EOF
 }
 
 footer() {
-  CHANGES=$(($(cat /tmp/.webif/config-* 2>&- | wc -l)))
-  CHANGES=${CHANGES#0}
-  CHANGES=${CHANGES:+(${CHANGES})}
+  _changes=$(($(cat /tmp/.webif/config-* 2>&- | wc -l)))
+  _changes=${_changes#0}
+  _changes=${_changes:+(${_changes})}
   cat <<EOF
                        </div>
                        <hr width="40%" />
@@ -138,14 +145,14 @@ footer() {
                <div id="save">
                        <div class="page-save">
                                <div>
-                                       $SAVEBUTTON
+                                       $_savebutton
                                </div>
                        </div>
                        <div class="apply">
                                <div>
-                                       <a href="config.sh?mode=save&amp;cat=$CATEGORY">Apply changes &laquo;</a><br />
-                                       <a href="config.sh?mode=clear&amp;cat=$CATEGORY">Clear changes &laquo;</a><br />
-                                       <a href="config.sh?mode=review&amp;cat=$CATEGORY">Review changes $CHANGES &laquo;</a>
+                                       <a href="config.sh?mode=save&amp;cat=$_category">Apply changes &laquo;</a><br />
+                                       <a href="config.sh?mode=clear&amp;cat=$_category">Clear changes &laquo;</a><br />
+                                       <a href="config.sh?mode=review&amp;cat=$_category">Review changes $_changes &laquo;</a>
                                </div>
                        </div>
                </div>
@@ -169,19 +176,49 @@ display_form() {
        echo "$1" | awk -F'|' -f /usr/lib/webif/form.awk
 }
 
-mkdir -p /tmp/.webif
+list_remove() {
+       echo "$1 " | awk '
+BEGIN {
+       RS=" "
+       FS=":"
+       first = 1
+}
+($0 !~ /^'"$2"'/) {
+       if (first != 1) printf " "
+       printf $0
+       first = 0
+}'
+}
+
+handle_list() {
+       _new="${1:+$(list_remove "$LISTVAL" "$1") }"
+       _new="${_new:-$LISTVAL}"
+       LISTVAL="$_new"
+       
+       _validate="$4"
+       _validate="${4:-none}"
+       [ \! -z "$3" ] && validate "$_validate|$2" && LISTVAL="$LISTVAL $2"
+
+       _changed="$1$3"
+       _return="${_changed:+0}"
+       _return="${_return:-255}"
+       LISTVAL="${LISTVAL# }"
+       LISTVAL="${LISTVAL%% }"
+       LISTVAL="${LISTVAL:- }"
+       return $_return
+}
 
 load_settings() {
        [ \! "$1" = "nvram" -a -f /etc/config/$1 ] && . /etc/config/$1
-       [ -f /tmp/.webif/config-$1 ] && . /tmp/.webif/config-$1 
+       [ -f /tmp/.webif/config-$1 ] && . /tmp/.webif/config-$1
 }
 
 validate() {
        eval "$(echo "$1" | awk -f /usr/lib/webif/validate.awk)"
-       [ -z "$ERROR" ] && return 0 || return 255
 }
 
 save_setting() {
+       mkdir -p /tmp/.webif
        oldval=$(eval "echo \${$2}")
        oldval=${oldval:-$(nvram get "$2")}
        mv /tmp/.webif/config-$1 /tmp/.webif/config-$1-old 2>&- >&-
index c3c5bfde9eab6758175933e26292636c955a4628..ddc44f62a139fc8d6c1b2ea3d4a269e7947dd688 100755 (executable)
@@ -4,33 +4,15 @@
 load_settings "wireless"
 
 
-WDS=${wl0_wds:-$(nvram get wl0_wds)}
-WDS="${WDS# }"
-[ -z "$FORM_wdsremove" ] || {
-       WDS=$(echo "$WDS " | awk '
-BEGIN {
-       RS=" "
-       FS=":"
-       first = 1
-}
-($0 !~ /^'"$FORM_wdsremove"'/) {
-       if (first != 1) printf " "
-       printf $0
-       first = 0
-}
-END { print "" }')
-       WDS="${WDS:- }"
-       save_setting wireless wl0_wds "$WDS"
-       FORM_submit=""
-}
-[ \! -z "$FORM_wdssubmit" ] && {
-       validate "mac|FORM_newmac|WDS MAC address||$FORM_newmac" && {
-               WDS="$WDS $FORM_newmac"
-               WDS="${WDS# }"
-               save_setting wireless wl0_wds "$WDS"
-       }
+FORM_wds="${wl0_wds:-$(nvram get wl0_wds)}"
+LISTVAL="$FORM_wds"
+handle_list "$FORM_wdsremove" "$FORM_wdsadd" "$FORM_wdssubmit" 'mac|FORM_wdsadd|WDS MAC address|required' && {
+       FORM_wds="$LISTVAL"
+       save_setting wireless wl0_wds "$FORM_wds"
        FORM_submit=""
 }
+FORM_wdsadd=${FORM_wdsadd:-00:00:00:00:00:00}
+
 CC=${wl0_country_code:-$(nvram get wl0_country_code)}
 case "$CC" in
        All|all|ALL) CHANNELS="1 2 3 4 5 6 7 8 9 10 11 12 13 14"; CHANNEL_MAX=14 ;;
@@ -43,7 +25,6 @@ for ch in $CHANNELS; do
 done
 
 if [ -z "$FORM_submit" ]; then
-       FORM_newmac=${FORM_newmac:-00:00:00:00:00:00}
        FORM_mode=${wl0_mode:-$(nvram get wl0_mode)}
        FORM_ssid=${wl0_ssid:-$(nvram get wl0_ssid)}
        FORM_channel=${wl0_channel:-$(nvram get wl0_channel)}
@@ -243,36 +224,12 @@ radio|key|$FORM_key|3
 text|key3|$FORM_key3|<br />
 radio|key|$FORM_key|4
 text|key4|$FORM_key4|<br />
+end_form
+start_form|WDS connections
+listedit|wds|$SCRIPT_NAME|$FORM_wds|$FORM_wdsadd
 end_form"
-?>
-<div class="settings">
-       <div class="settings-title"><h3><strong>WDS connections</strong></h3></div>
-       <div class="settings-content">
-       <table summary="Settings" width="100%">
-<?
-echo "$WDS " | awk '
-BEGIN {
-       RS=" "
-       FS=":"
-}
-
-$0 ~ /^[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]$/ {
-       print "<tr><td width=\"50%\">" $0 "</td><td>&nbsp;<a href=\"'"$SCRIPT_NAME"'?wdsremove=" $0 "\">Remove</a></td></tr>"
-}
-'
-?>
-               <tr>
-                       <td width="45%"><input type="text" name="newmac" value="<? echo -n $FORM_newmac ?>" /></td>
-                       <td width="55%"><input type="submit" name="wdssubmit" value="Add WDS peer" /></td>
-               </tr>
-       </table>
-       </div>
-       <div class="settings-help">&nbsp;</div>
-</div>
-<div style="clear: both">&nbsp;</div>
-
 
-<? footer ?>
+footer ?>
 <!--
 ##WEBIF:name:Network:3:Wireless
 -->